local skel = fk.CreateSkill {
  name = "lb__paozhang",
  tags = { Skill.Compulsory },
  derived_piles = {"$lb__paozhang", "lb__paozhang-cards"}
}

Fk:loadTranslationTable {
  ["lb__paozhang"] = "炮仗",
  [":lb__paozhang"] = "<b>锁定技</b>，准备阶段，若你没有「<b>炮仗</b>」，将牌堆顶的一张牌扣置于你的武将牌上称为「<b>炮仗</b>」（仅你可见）；<br>" ..
      "当有牌因使用而进入弃牌堆时，使用者将此牌置于「<b>炮仗</b>」旁；<br>" ..
      "每回合结束时，当前回合角色须将牌堆顶的一张牌置于「<b>炮仗</b>」旁；<br>" ..
      "当「<b>炮仗</b>」旁的牌数等于「<b>炮仗</b>」的点数时，你弃置「<b>炮仗</b>」与「<b>炮仗</b>」旁的牌并对最后一名将牌置于「<b>炮仗</b>」旁的角色造成2点火焰伤害，" ..
      "若该角色为你，你可以先获得「<b>炮仗</b>」旁的至多三张牌。",

  ["#lb__paozhang_1"] = "炮仗",
  ["#lb__paozhang_2"] = "炮仗",
  ["#lb__paozhang_3"] = "炮仗",
  ["@lb__paozhang"] = "炮仗",
  ["$lb__paozhang"] = "炮仗",
  ["lb__paozhang-cards"] = "炮仗牌",
  ["#lb__paozhang-xuan"] = "炮仗：你可以获得至多三张<b>炮仗</b>牌",
  

  ["$lb__paozhang1"] = "有钱的捧个钱场！",
  ["$lb__paozhang2"] = "没钱的下次一定！",
  ["$lb__paozhang3"] = "瞧一瞧看一看了哎！",
  ["$lb__paozhang4"] = "恭喜发财！",
  ["$lb__paozhang5"] = "花开富贵！",
  ["$lb__paozhang6"] = "幸亏我练过…",
}

skel:addEffect(fk.AfterCardsMove, {
  audio_index = {4, 5},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    for _, move in ipairs(data) do
      if move.toArea == Card.PlayerSpecial and move.skillName == "lb__paozhang-cards" then
        event:setCostData(self, { tos = { move.proposer } })
        local c = player:getPile("$lb__paozhang")
        if #c > 0 then --避免一次有两张牌进入炮仗牌时报错
          return Fk:getCardById(c[1]).number == #player:getPile("lb__paozhang-cards")
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCardTo(player:getPile("$lb__paozhang"), Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "lb__paozhang",
      "$lb__paozhang", true, player)
    local t = event:getCostData(self).tos[1]
    if t == player then
      local cs = room:askToCards(player, {
        min_num = 1,
        max_num = 3,
        pattern = tostring(Exppattern { id = player:getPile("lb__paozhang-cards") }),
        skill_name = skel.name,
        prompt = "#lb__paozhang-xuan",
        expand_pile = "lb__paozhang-cards",
        cancelable = true,
      })
      if #cs > 0 then
        player:broadcastSkillInvoke(skel.name, 6)
        room:notifySkillInvoked(player, skel.name, "masochism")
        room:obtainCard(player, cs, true, fk.ReasonJustMove, player, "lb__paozhang")
      end
    end
    room:moveCardTo(player:getPile("lb__paozhang-cards"), Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "lb__paozhang",
      "lb__paozhang-cards", true, player)
    --room:setPlayerMark(player,"@lb__paozhang",0)
    room:damage({
      who = player,
      to = t,
      damage = 2,
      damageType = fk.FireDamage,
      skillName = "lb__paozhang",
    })
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  is_delay_effect = true,
  audio_index = 1,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Start and
    #player:getPile("$lb__paozhang") == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local c = room:getNCards(1)[1]
    --room:setPlayerMark(player,"@lb__paozhang",Fk:getCardById(c).number)
    player:addToPile("$lb__paozhang", c, false, "lb__paozhang", player)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if (not player:hasSkill(skel.name)) or (#player:getPile("$lb__paozhang") == 0) then return false end
    local room = player.room
    local move_event = room.logic:getCurrentEvent()               --当前时机
    local parent_event = move_event:findParent(GameEvent.UseCard) --当前时机的父级时机
    local use_zu = {}
    local use_t = {}
    if parent_event ~= nil then
      if parent_event.event == GameEvent.UseCard then
        local parent_data = parent_event.data --使用
        for _, id in ipairs(room:getSubcardsByRule(parent_data.card)) do
          table.insert(use_zu, { cid = id, from = parent_data.from })
        end
      end
    end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile and #use_zu > 0 then --使用进入弃牌堆
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.Processing then
            for _, v in ipairs(use_zu) do
              if v.cid == info.cardId then
                table.insert(use_t, v)
              end
            end
          end
        end
      end
    end
    if #use_t > 0 then
      event:setCostData(self, { tcs = use_t })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      for _, t in ipairs(event:getCostData(self).tcs) do
        if t.from == p and #player:getPile("$lb__paozhang") > 0 then
          --player:broadcastSkillInvoke(paozhang.name,2)
          player:addToPile("lb__paozhang-cards", t.cid, true, "lb__paozhang-cards", p)
        end
      end
    end
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target.phase == Player.Finish and #player:getPile("$lb__paozhang") > 0
  end,
  on_use = function(self, event, target, player, data)
    --player:broadcastSkillInvoke(paozhang.name,3)
    local room = player.room
    player:addToPile("lb__paozhang-cards", room:getNCards(1), true, "lb__paozhang-cards", target)
  end,
})

skel:addEffect("visibility", {
  card_visible = function (self, player, card)
    if player:getPileNameOfId(card.id) == "$lb__paozhang" then
      return true
    end
  end
})

return skel